﻿KISSY.use("validation, fgt/auth, fgt/ui", function (S, Validation, Auth, UI) {
    S.ready(function (S) {
        var $ = S.all,
            J = jQuery,
            form = $("#J_Interior"),
            VALID_EFFECT = "Simple",
            VALID_STYLE = "s_b",
            validator = new Validation(form[0], { warn: VALID_EFFECT, style: VALID_STYLE }),
            submitBtn = $("#J_Sumbit"),
            popbox = UI.getPopbox();

        validator.add("#sleeps",
        {
            required: [true, "Sleeps is required."]
        });

        validator.add("#width",
        {
            required: [false],
            regex: [/^\d+(\.\d+)?$/, 'Must be numeric.'],
            moreThan: [0, "Must be more than {0} FT."]
        });

        validator.add("#length",
        {
            required: [false],
            regex: [/^\d+(\.\d+)?$/, 'Must be numeric.'],
            moreThan: [0, "Must be more than {0} FT."]
        });

        validator.add("#black_water_tank",
        {
            required: [false],
            regex: [/^\d+(\.\d+)?$/, 'Must be numeric.'],
            moreThan: [0, "Must be more than {0} FT."]
        });

        validator.add("#grey_water_tank",
        {
            required: [false],
            regex: [/^\d+(\.\d+)?$/, 'Must be numeric.'],
            moreThan: [0, "Must be more than {0} FT."]
        });

        validator.add("#fresh_water_tank",
        {
            required: [false],
            regex: [/^\d+(\.\d+)?$/, 'Must be numeric.'],
            moreThan: [0, "Must be more than {0} FT."]
        });

        validator.add("#water_heater",
        {
            required: [false],
            regex: [/^\d+(\.\d+)?$/, 'Must be numeric.'],
            moreThan: [0, "Must be more than {0} FT."]
        });

        validator.add("#propane_tanks",
        {
            required: [false],
            regex: [/^\d+(\.\d+)?$/, 'Must be numeric.'],
            moreThan: [0, "Must be more than {0} FT."]
        });

        function submit() {
            Auth.validate(function () {
                try {
                    var paramStr = S.ajax.serialize(form),
                        resultCode = FGT.core.constant.jsonResultCode,
                        failureText = "Save failure, please try again later.",
                        delay = 1000;

                    J.ajax({
                        url: "/rv/save/interior",
                        type: "POST",
                        data: paramStr,
                        beforeSend: function (jqXHR, settings) {
                            submitBtn.prop("disabled", true);
                            popbox.show("Saving");
                        },
                        success: function (data, textStatus, jqXHR) {
                            if (data.Result === resultCode.SUCCESS) {
                                popbox.show(data.Content, delay);
                            }
                            else {
                                var jsonData = data.Content;
                                if (S.isString(jsonData)) {
                                    popbox.show(jsonData, delay);
                                }
                                else if (S.isArray(jsonData)) {
                                    S.each(jsonData, function (item) {
                                        var target = $(item.id);
                                        if (target.attr("data-msgbox")) {
                                            var label = $(target.attr("data-msgbox"));
                                            if (label) {
                                                label.text(item.msg);
                                                label.removeClass("ok tip error");
                                                label.addClass("error");
                                                label.show();
                                            }
                                        }
                                    });
                                    popbox.hide();
                                }
                                submitBtn.prop("disabled", false);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            submitBtn.prop("disabled", false);
                            popbox.show(failureText, delay);
                        }
                    });
                }
                catch (e) {
                    popbox.show(failureText, delay);
                    submitBtn.prop("disabled", false);
                }
            }, popbox, this);
        }

        submitBtn.on("click", function (event) {
            event.halt();
            var self = this,
                b = validator.isValid();
            if (b === true) {
                submit();
            }
        });
    });
});